package com.tarena.seata.order.service;

import com.tarena.seata.api.CartApi;
import com.tarena.seata.api.StockApi;
import com.tarena.seata.order.mapper.OrderMapper;
import com.tarena.seata.order.pojo.OrderPO;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private StockApi stockApi;
    @Autowired
    private CartApi cartApi;
    @GlobalTransactional
    public void createOrder() {
        System.out.println("seata全局事务id=============="+ RootContext.getXID());
        stockApi.reduceStock("PC101",5);
        OrderPO order = new OrderPO();
        // 随机或随意赋值示例
        order.setId(12345);
        order.setOrderSn("ORDER20230401001");
        order.setUserId("USER123456");
        order.setProductCode("PC101");
        order.setCount(5);
        order.setTotalMoney(100);
        orderMapper.insert(order);
        cartApi.delCart("UU100","P100");
    }
    @GlobalTransactional
    public void createOrder1() {
        System.out.println("seata全局事务id=============="+ RootContext.getXID());
        stockApi.reduceStock("PC101",5);
        OrderPO order = new OrderPO();
        // 随机或随意赋值示例
        order.setId(12345);
        order.setOrderSn("ORDER20230401001");
        order.setUserId("USER123456");
        order.setProductCode("PC101");
        order.setCount(5);
        order.setTotalMoney(100);
        orderMapper.insert(order);
        cartApi.delCart("UU100","P100");
    }
}
